//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyq.bean.service;

import com.lyq.bean.Course;
import com.lyq.bean.Student;
import com.lyq.bean.mySql.AddC_Sql;
import com.lyq.bean.mySql.RemoveC_Sql;
import com.lyq.bean.mySql.SeekAllC_Sql;
import com.lyq.bean.mySql.SeekC_Sql;
import com.lyq.bean.mySql.SeekExist_Sql;
import com.lyq.bean.mySql.UpdateC_Sql;
import java.util.List;

public class C_Service {
    public C_Service() {
    }

    public Boolean add_C(String No, String name, String time, String place, String credit, String teacher,String limitnum,String courseType, String campus, String category,int major_id,int year_id) {
        Boolean bool = (new SeekExist_Sql()).seekExist_Sql(No, "课程");
        if (bool) {
            bool = false;
        } else if (!bool) {
            bool = (new AddC_Sql()).addC_Sql(No, name, time, place, credit, teacher,limitnum, courseType, campus,category,major_id,year_id );
        }

        return bool;
    }
    public List<Course> seekAll_C(List<Course> list) {
        List<Course> lt = (new SeekAllC_Sql()).seekAllC_Sql(list);
        return lt;
    }

    public Boolean remove_C(String cNo) {
        // 先删除所有学生的选课记录
        List<Student> list = (new TC_Service()).seekElectS(cNo);
        boolean allUnenrolled = true;

        for(int i = 0; i < list.size(); ++i) {
            String sNo = ((Student)list.toArray()[i]).getNo();
            Boolean unenrollResult = (new E_Service()).recede_C(cNo, sNo);
            if (!unenrollResult) {
                allUnenrolled = false;
            }
        }

        // 然后删除课程本身
        Boolean courseRemoved = (new RemoveC_Sql()).removeC_Sql(cNo);

        return courseRemoved && allUnenrolled;
    }


    public List<Course> seek_C(String select, String seek, String name, List<Course> list) {
        if (select.equals("编号")) {
            select = "C_No";
        } else if (select.equals("名称")) {
            select = "C_Name";
        } else if (select.equals("授课教师")) {
            select = "C_Teacher_ID";
        }

        List<Course> lt = (new SeekC_Sql()).seekC_Sql(select, seek, name, list);
        return lt;
    }

    //写一个获取单个课程的方法
    public Course seek_CS(String cNo) {
        return (new SeekC_Sql()).getCourseById(cNo);
    }//新增方法
    public Boolean update_C(String No, String time, String place, String credit) {
        Boolean bool = (new UpdateC_Sql()).updateC_Sql(No, time, place, credit);
        return bool;
    }
}